<template>
	<view class="content">
		<view class="btn" @tap="change" :style="{backgroundColor:bgc}">
			点击我
		</view>
		<view class="upload" @tap="showindex">
			显示用户和群列表(首页展示)
		</view>
		<view class="upload" @tap="createuser">
			用户注册
		</view>
		<view class="upload" @tap="usercheck">
			用户名字邮箱验证是否重复
		</view>
		<view class="upload" @tap="loginmatch">
			用户登录验证
		</view>
		<view class="upload" @tap="search">
			搜索
		</view>
		<view class="upload" @tap="upload">
			文件上传
		</view>
		<image v-for="(item) in imglist " :src="item"></image>
		<view class="upload" @tap="creategroup">
			建群
		</view>
		<view class="upload" @tap="insertroupmsg">
			写入群消息
		</view>
		
		<view class="upload" @tap="addfriendrequest">
			好友申请
		</view>
		<view class="upload" @tap="disagree">
			拒绝
		</view>
		<view class="upload" @tap="agree">
			同意
		</view>
		<view class="upload" @tap="judgefriend">
			判断是否为好友
		</view>
	</view>
</template>

<script>
	import myswich from "../../components/switch/switch.vue"
	export default {
		data() {
			return{
				bgc:'',
				id:'adqdqwudadhiuh9qdhad',
				imglist:[],
				indexlist:[],//用于保存好友列表和群列表的时间排序
			}
		},
		onLoad() {
			
		},
		methods: {
			judgefriend(){
				uni.request({
					url:"http://localhost:3000/search/isfriend",
					data:{
						uid:'61054e7184d6343168fd1568',
						fid:'610f2ed680fe595434c75c46'
					},
					method:'POST',
					success:(res)=>{					
						console.log(res.data.data)
					}
				})
			},
			async showindex(){//显示首页好友和群列表
				let userlist = await this.showuserlist().then(data => data);
				let grouplist = await this.showgrouplist().then(data => data);
				this.indexlist = userlist.concat(grouplist)		
				this.indexlist.sort(function(a,b){//主页获取好友和群之后根据最新的消息进行时间的排序
					let a_time = (new Date(a.lasttime)).getDate()
					let b_time = (new Date(b.lasttime)).getDate()
					return a_time-b_time
				})
				console.log(this.indexlist)
			},
			showuserlist(){//获取好友列表
				return new Promise((resolve,reject)=>{
					uni.request({
						url:"http://localhost:3000/index/getusers",
						data:{
							uid:'61054e7184d6343168fd1568',
						},
						method:'POST',
						success:(res)=>{					
							this.indexlist = res.data.data;
							resolve(res.data.data)
						}
					})
				})
				
			},
			showgrouplist(){//获取群
				return new Promise((resolve,reject)=>{
					uni.request({
						url:"http://localhost:3000/index/getgroups",
						data:{
							uid:'61054e7184d6343168fd1568',
						},
						method:'POST',
						success:(res)=>{					
							resolve(res.data.data)
						}
					})
				})
			},
			addfriendrequest(){//添加好友申请
				uni.request({
					url:"http://localhost:3000/friend/applyfriend",
					data:{
						uid:'6105f987f0afd43abc743851',
						fid:'61054e7184d6343168fd1568',
						message:'周陈凯向wuda123发出好友的申请'
					},
					method:'POST',
					success:(res)=>{
						console.log(res)
					}
				})
			},
			disagree(){//拒绝
				uni.request({
					url:"http://localhost:3000/friend/deletefriend",
					data:{				
						uid:'610551cf84d6343168fd1576',
						fid:'61054e7184d6343168fd1568',
					},
					method:'POST',
					success:(res)=>{
						console.log(res)
					}
				})
			},
			agree(){//同意申请
				uni.request({
					url:"http://localhost:3000/friend/updatefriendstate",
					data:{				
						uid:'61054e7184d6343168fd1568',
						fid:'610551cf84d6343168fd1576',
					},
					method:'POST',
					success:(res)=>{
						console.log(res)
					}
				})
			},
			change(){
				uni.navigateTo({
					url:"../llogin/llogin"			
				})
			},
			createuser(){//添加用户(用户注册)
				uni.request({
					url:"http://localhost:3000/siginup/add",
					data:{
						name:'武大3',
						email:'223we3@qq.com',
						psw:'123456789'
					},
					method:'POST',
					success:(data)=>{
						console.log(data)
					}
				})
			},
			usercheck(){//验证用户名和邮箱是否已存在
				uni.request({
					url:"http://localhost:3000/siginup/judge",
					data:{
						data:'240046220@qq.com',
						type:'email',
					},
					method:'POST',
					success:(data)=>{
						console.log(data)
					}
				})
			},
			loginmatch(){//登录验证
				uni.request({
					url:"http://localhost:3000/siginin/match",
					data:{
						data:'zck',
						psw:'123456789'
					},
					method:'POST',
					success:(data)=>{
						console.log(data)
					}
				})
			},
			search(){//搜索
				uni.request({
					url:"http://localhost:3000/search/searchinfo",
					data:{
						uid:'61054e7184d6343168fd1568',
						key:'2400'
					},
					method:'POST',
					success:(data)=>{
						console.log(data)
					}
				})
			},
			creategroup(){//建群
				uni.request({
					url:"http://localhost:3000/group/creategroup",
					data:{
						uid:"61054e7184d6343168fd1568",
						name:"新建的第一个群",
						imgurl:"阿三大旗网大旗网的",
						myusers:["61054e7184d6343168fd1568","610551cf84d6343168fd1576"]
					},
					method:'POST',
					success:(result)=> {
						console.log(result)
					}
				})
			},
			insertroupmsg(){//添加群信息
				uni.request({
					url:"http://localhost:3000/group/insertgroupmsg",
					data:{
						gid:"6105f19c28235f3dc49ff57f",
						uid:"61054e7184d6343168fd1568",
						message:"群主发的消息",
						types:0,
					},
					method:'POST',
					success:(result)=> {
						console.log(result)
					}
				})
			},
			upload(){//上传文件
				uni.chooseImage({
					count: 6, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
				    success: (chooseImageRes) => {
				        const tempFilePaths = chooseImageRes.tempFilePaths;
						for(var i = 0;i<tempFilePaths.length;i++){//上次多张图片
							const uploadTask = uni.uploadFile({
							    url: 'http://localhost:3000/files/upload', //仅为示例，非真实的接口地址
							    filePath: tempFilePaths[i],
							    name: 'file',
							    formData: {
							        'user': 'wiki',
									'url':'needdir',
									'name':new Date().getTime() + this.id + i//命名规则时间戳+用户id+随机数
							    },
							    success: (uploadFileRes) => {
									let path = 'needdir/'
									let obj = JSON.parse(uploadFileRes.data)
									this.imglist.push(`http://localhost:3000/${path}/${obj.filename}`)
									console.log(obj)
							    }
							});	
							uploadTask.onProgressUpdate((res) => {
								console.log('上传进度' + res.progress);
								console.log('已经上传的数据长度' + res.totalBytesSent);
								console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
											
								// 测试条件，取消上传任务。
								// if (res.progress > 50) {
								//     uploadTask.abort();
								// }
							});
						}	                
				    }
				});
			}
		},
		components:{
			myswich
		}
	}
</script>

<style lang="scss">
	.content{
		.btn{
			width: 200rpx;
			height: 200rpx;
			background-color: red;
		}
		.upload{
			height: 50rpx;
			padding: 20rpx;
			background-color: gray;
			text-align: center;
			font-size: 36rpx;
		}
	}
</style>
